// src/api/OrderApi.js
import instance from '@/utils/request';
import { ElMessage } from 'element-plus';

/**
 * 获取用户的收货地址列表
 * @returns {Promise<Object>} 返回服务器响应的Promise
 */
export const getAddressList = () => {
    return instance({
        url: '/back/orders/address/list',
        method: 'GET'
    });
};

/**
 * 根据选中的购物车商品和地址、支付方式创建订单
 * @param {Object} orderData - 订单数据，包含 cartIds, addressId, paymentType, remark
 * @returns {Promise<Object>} 返回服务器响应的Promise，包含新创建的订单信息
 */
export const createOrder = (orderData) => {
    return new Promise((resolve, reject) => {
        instance({
            url: '/back/orders/operation/create',
            method: 'POST',
            data: orderData,
        }).then(response => {
            if (response.data.code === 1) {
                resolve(response.data);
            } else {
                ElMessage.error(response.data.msg || '创建订单失败！');
                reject(response.data);
            }
        }).catch(error => {
            reject(error);
        });
    });
};